home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter6 / hittest.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  10KB  |  288 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "hittest.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "ListView_HitTest()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LPCTSTR lpszData[4]  = { "Circle", "Rectangle", "Cross", "Check" };
  108. LPCTSTR lpszColor[4] = { "Yellow", "Red",       "Black", "Black" };
  109.  
  110. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  111. {
  112. static char  szBuf[40];
  113. static UINT  uTimerID = 0;
  114. static HWND  hList    = NULL;
  115. static HWND  hStatic  = NULL;
  116.  
  117.    switch( uMsg )
  118.    {
  119.       case WM_CREATE  :
  120.               {
  121.                  HIMAGELIST hImage, hSmall;
  122.  
  123.                  InitCommonControls();
  124.  
  125.                  // Create the image lists and the list view.
  126.                  //..........................................
  127.                  hImage = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  128.                  hSmall = ImageList_Create( 16, 16, ILC_COLOR4 | ILC_MASK, 4, 1 );
  129.                  hList  = CreateWindowEx( 0, WC_LISTVIEW, "",
  130.                                           WS_CHILD | WS_VISIBLE | LVS_REPORT | 
  131.                                           LVS_EDITLABELS | LVS_SINGLESEL,
  132.                                           0, 20, 100, 100,
  133.                                           hWnd,
  134.                                           (HMENU)1,
  135.                                           hInst,
  136.                                           NULL);
  137.  
  138.                  if ( hList && hImage && hSmall )
  139.                  {
  140.                     LV_COLUMN col;
  141.                     LV_ITEM   item;
  142.                     int       i;
  143.  
  144.                     // Set the large and small image lists.
  145.                     //.....................................
  146.                     ListView_SetImageList( hList, hImage, LVSIL_NORMAL );
  147.                     ListView_SetImageList( hList, hSmall, LVSIL_SMALL );
  148.  
  149.                     // Add the images to the image list.
  150.                     //..................................
  151.                     for( i=0; i<4; i++ )
  152.                     {
  153.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  154.                        ImageList_AddIcon( hSmall, LoadIcon( hInst, lpszData[i] ) );
  155.                     }
  156.  
  157.                     // Add the columns for the report view.
  158.                     //.....................................
  159.                     col.mask    = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 
  160.                     col.fmt     = LVCFMT_LEFT; 
  161.                     col.cx      = 100; 
  162.  
  163.                     col.pszText  = "Name"; 
  164.                     col.iSubItem = 0;
  165.                     ListView_InsertColumn( hList, 0, &col );
  166.                     
  167.                     col.pszText  = "Color"; 
  168.                     col.iSubItem = 1;
  169.                     ListView_InsertColumn( hList, 1, &col );
  170.  
  171.                     // Insert items into the list view.
  172.                     //.................................
  173.                     for( i=0; i<4; i++ )
  174.                     {
  175.                        item.mask     = LVIF_TEXT | LVIF_IMAGE;
  176.                        item.pszText  = (LPTSTR)lpszData[i];
  177.                        item.iItem    = i;
  178.                        item.iSubItem = 0;
  179.                        item.iImage   = i;
  180.                        ListView_InsertItem( hList, &item );
  181.  
  182.                        item.mask     = LVIF_TEXT;
  183.                        item.pszText  = (LPTSTR)lpszColor[i];
  184.                        item.iSubItem = 1;
  185.                        ListView_SetItem( hList, &item );
  186.                     }
  187.                  }
  188.  
  189.                  uTimerID = SetTimer( hWnd, 1, 500, NULL );
  190.               }
  191.               break;
  192.  
  193.       case WM_SIZE :
  194.               if ( hList )
  195.                  MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  196.  
  197.               if ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED )
  198.                  ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
  199.               break;
  200.  
  201.       case WM_CONTEXTMENU :
  202.               {
  203.                  LV_HITTESTINFO hi;
  204.                  HMENU          hMenu, hSubMenu;
  205.  
  206.                  hi.pt.x = LOWORD(lParam);
  207.                  hi.pt.y = HIWORD(lParam);
  208.  
  209.                  // Convert the screen coordinates to client.
  210.                  //..........................................
  211.                  ScreenToClient( hList, &hi.pt );
  212.                  
  213.                  // Check if an item was hit.
  214.                  //..........................
  215.                  if ( ListView_HitTest( hList, &hi ) > -1 )
  216.                     hMenu = LoadMenu( hInst, "ITEMPOPUPMENU" );
  217.                  else
  218.                     hMenu = LoadMenu( hInst, "POPUPMENU" );
  219.  
  220.                  // Get the submenu to display.
  221.                  //............................
  222.                  hSubMenu = GetSubMenu( hMenu, 0 );
  223.  
  224.                  TrackPopupMenu( hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
  225.                                  LOWORD( lParam ), HIWORD( lParam ), 0,
  226.                                  hWnd, NULL );
  227.  
  228.                  DestroyMenu( hMenu );
  229.               }
  230.               break;
  231.  
  232.       case WM_COMMAND :
  233.               switch( LOWORD( wParam ) )
  234.               {
  235.                  case IDM_ABOUT :
  236.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  237.                         break;
  238.  
  239.                  case IDM_EXIT :
  240.                         DestroyWindow( hWnd );
  241.                         break;
  242.               }
  243.               break;
  244.       
  245.       case WM_DESTROY :
  246.               if ( hList )
  247.               {
  248.                  if ( ListView_GetImageList( hList, LVSIL_NORMAL ) )
  249.                     ImageList_Destroy( ListView_GetImageList( hList, LVSIL_NORMAL ) );
  250.  
  251.                  if ( ListView_GetImageList( hList, LVSIL_SMALL ) )
  252.                     ImageList_Destroy( ListView_GetImageList( hList, LVSIL_SMALL ) );
  253.               }
  254.  
  255.               PostQuitMessage(0);
  256.               break;
  257.  
  258.       default :
  259.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  260.    }
  261.  
  262.    return( 0L );
  263. }
  264.  
  265.  
  266. LRESULT CALLBACK About( HWND hDlg,           
  267.                         UINT message,        
  268.                         WPARAM wParam,       
  269.                         LPARAM lParam)
  270. {
  271.    switch (message) 
  272.    {
  273.        case WM_INITDIALOG: 
  274.                return (TRUE);
  275.  
  276.        case WM_COMMAND:                              
  277.                if (   LOWORD(wParam) == IDOK         
  278.                    || LOWORD(wParam) == IDCANCEL)    
  279.                {
  280.                        EndDialog(hDlg, TRUE);        
  281.                        return (TRUE);
  282.                }
  283.                break;
  284.    }
  285.  
  286.    return (FALSE); 
  287. }
  288.